<?php
/**
 * bs多规格sku商城系统
 * ============================================================================
 * 版权所有 2024-2099 wltx666.com，并保留所有权利。
 * 网站地址: https://wltx666.com
 * ============================================================================
 * Author: 草上飞
 * Email: wltx@alipay168.cn
 * QQ: 2719875001
 * Date: 2025/05/11
 * 描述: api_xxx 代表前端接口,其它代表后端接口
 */

namespace app\result\auto;

use app\model\auto\Invoice;
use app\result\BaseResult;


class InvoiceResult extends BaseResult
{
    public function __construct()
    {
        parent::__construct();
        $this->model = new Invoice();
    }

    // 获取列表
    public function api_list($user_id, $args = []): array
    {
        $page = max($args['page'] ?? 1, 1);
        $page_size = max($args['page_size'] ?? 10, 1);
        $keyword = trim($args['keyword'] ?? '');
        if ($page_size > 100) {
            $page_size = 100;
        }
        $where = [
            ['user_id', '=', $user_id],
        ];
        if (!empty($keyword)) {
            //$where[] = ['title', 'like', '%' .$keyword . '%'];
        }

        $total = $this->model->data_count($where);
        $data = $this->model->data_list($where, '*', 'invoice_id desc', $page, $page_size);
        if ($data) {
            //todo 自己的逻辑
        }

        return self::success_return('success', [
            'total' => $total,
            'list' => $data,
        ]);
    }

    //详情
    public function api_get($user_id, $args = []): array
    {
        $id = intval($args['invoice_id'] ?? 0);
        if (!$id) {
            return self::error_return('参数错误');
        }
        $data = $this->model->data_get(['user_id' => $user_id, 'invoice_id' => $id]);
        if (!$data) {
            return self::error_return('数据不存在');
        }

        return self::success_return('success', $data);
    }

    //增改操作
    public function api_save_data($user_id, $args = []): array
    {
        $id = intval($args['invoice_id'] ?? 0);
        if ($id) {
            $data = $this->model->data_get(['user_id' => $user_id, 'invoice_id' => $id]);
            if (!$data) {
                return self::error_return('数据不存在');
            }
        }

        $rules = [
            ['title', 'empty|max:120', '请填写发票抬头|发票抬头不能大于120个字'],
            ['tax_number', 'empty', '请填写纳税人识别号'],
        ];
        $check = check_param($args, $rules);
        if ($check['status'] != 0) {
            return self::error_return($check['msg']);
        }

        $baseData = [
            'user_id' => $user_id,
            'title' => $args['title'],
            'tax_number' => $args['tax_number'],
            'mobile' => $args['mobile'] ?? '',
            'email' => $args['email'] ?? '',
            'is_default' => !empty($args['is_default']) ? 1 : 0,
            'type' => $args['type'] ?? 0,
            'tax_rate' => $args['tax_rate'] ?? 0,
            'bank_name' => $args['bank_name'] ?? '',
            'bank_account' => $args['bank_account'] ?? '',
            'address' => $args['address'] ?? '',

        ];

        if ($id) {
            $this->model->data_update(['invoice_id' => $id], $baseData);
        } else {
            $baseData['user_id'] = $user_id;
            $this->model->data_add($baseData);
        }
        if ($args['is_default'] ?? 0) {
            $this->model->data_update(['user_id' => $user_id], ['is_default' => 0]);
            $this->model->data_update(['invoice_id' => $id], ['is_default' => 1]);
        }
        return self::success_return('保存成功');
    }

    //删除数据
    public function api_delete($user_id, $args = []): array
    {
        $id = intval($args['invoice_id'] ?? 0);
        if (!$id) {
            return self::error_return('参数错误');
        }
        $data = $this->model->data_get(['user_id' => $user_id, 'invoice_id' => $id]);
        if (!$data) {
            return self::error_return('地址不存在');
        }
        $this->model->data_del(['invoice_id' => $id]);
        return self::success_return('操作成功');
    }

    //获取下拉列表
    public function api_list_options($user_id, $args = []): array
    {
        $all = $this->model->data_list_nopage(['user_id' => $user_id], 'title,invoice_id,tax_number', 'is_default desc');
        if ($all) {
            foreach ($all as &$item) {
                $item['label'] = $item['title'];
                $item['value'] = $item['invoice_id'];
            }
        }
        return self::success_return('success', [
            'list' => $all,
        ]);
    }

    //设置默认
    public function api_set_default($user_id, $args = [])
    {
        $id = intval($args['invoice_id'] ?? 0);
        if (!$id) {
            return self::error_return('参数错误');
        }
        $data = $this->model->data_get(['user_id' => $user_id, 'invoice_id' => $id]);
        if (!$data) {
            return self::error_return('地址不存在');
        }
        $this->model->data_update(['user_id' => $user_id], ['is_default' => 0]);
        $this->model->data_update(['invoice_id' => $id], ['is_default' => 1]);
        return self::success_return('设置成功');
    }

}
